function Game(el,a={}){    
    this.a=a
    //保存地图对象
    this.el=document.querySelector(el)
    //
    this.t=null
    //搞地图
    this.map=new Map(this.el)
    this.map.create()

    //搞食物
    this.food=new Food(this.el)
    this.food.create()

    //搞蛇
    this.snake=new Snake(this.el,)
    this.snake.create()
    //开始
    this.starton()
}
Game.prototype.starton=function(){
    let start=document.querySelector('.start')
    start.onclick=()=>{
        let abc=prompt('请输入速度，建议（请输入速度，建议50-2000ms）')
        if(!(abc<50||abc>2000)){
            
        if(this.t) clearInterval(this.t)
        this.t=setInterval(()=>{
        //蛇动
        this.snake.move()
        //判断吃食
        let snakeX=this.snake.snakeobj[0].x*20
        let snakeY=this.snake.snakeobj[0].y*20
        let foodX=this.food.foodX
        let foodY=this.food.foodY
        if(snakeX==foodX&&snakeY==foodY){
            let last=this.snake.snakeobj[this.snake.snakeobj.length -1]
            this.snake.snakeobj.push({
                x:last.x,
                y:last.y,
                color:last.color,
                el:null
            })
            this.snake.create()//重新绘制
            this.food.move()
        }
        


        // console.log(this.snake.move);
        },abc)

        let stop1=document.querySelector('.stop')
            stop1.onclick=()=>{
                clearInterval(this.t)
            }



        document.addEventListener('keydown',(e)=>{
             e=e ||window.event
            // console.log(e.keyCode);
            // alert('牛逼')
            switch(e.keyCode){
                case 38:
                    this.snake.direction='top'
                    break;
                case 40:
                    this.snake.direction='bottom'
                    break;
                case 37:
                    if(this.snake.direction=="right"){
                        alert('游戏结束')
                    }
                    this.snake.direction='left'
                    break;
                case 39:
                    this.snake.direction='right'
                    break;
                    default:
                        break;

            }



        })
    }else{
            alert('碧池，别乱输入')
        }

    }
}


//////////////////////////////////////////
